package com.atguigu.day02;


import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * flatMap的使用
 */
public class Example4 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        env
                .fromElements("zhang3","li4","wang5")
                .flatMap(new FlatMapFunction<String, String>() {
                    @Override
                    public void flatMap(String s, Collector<String> collector) throws Exception {
                        if (s.equals("zhang3"))
                            collector.collect(s);
                        else if (s.equals("li4")){
                            collector.collect(s);
                            collector.collect(s);
                        }
                    }
                })
                .print();
        env
                .fromElements("zhang3","li4","wang5")
                .flatMap((String s,Collector<String> out)->{
                    if (s.equals("zhang3"))
                        out.collect(s);
                    else if (s.equals("li4")){
                        out.collect(s);
                        out.collect(s);
                    }
                }).returns(Types.STRING)
                .print();
        env.execute();
    }
}
